package com.android.hesapci;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.Vector;

import android.util.Xml;
import org.w3c.dom.Attr;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xmlpull.v1.XmlSerializer;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * Created by IntelliJ IDEA.
 * User: dzhoshkun
 * Date: 11/22/11
 * Time: 9:21 PM
 * To change this template use File | Settings | File Templates.
 */
public class UserData implements Serializable {
    private String name;
    private Date dateReachedResponsibility;
    private Date dateStartedPrayer;
    private Date dateStartedFasting;
    public static final int YEAR_OFFSET=1900;

    private UserData() {}

    public UserData(String name, Date dateReachedResponsibility, Date dateStartedPrayer, Date dateStartedFasting) throws InvalidTimeDifferenceException {
        if (dateReachedResponsibility.getYear() > dateStartedPrayer.getYear() ||
                dateReachedResponsibility.getYear() > dateStartedFasting.getYear())
            throw new InvalidTimeDifferenceException();
        if (dateReachedResponsibility.getYear() == dateStartedPrayer.getYear()) {
            if (dateReachedResponsibility.getMonth() > dateStartedPrayer.getMonth())
                throw new InvalidTimeDifferenceException();
            else if (dateReachedResponsibility.getMonth() == dateStartedPrayer.getMonth())
                if (dateReachedResponsibility.getDay() > dateStartedPrayer.getDay())
                    throw new InvalidTimeDifferenceException();
        }
        if (dateReachedResponsibility.getYear() > dateStartedFasting.getYear())
            throw new InvalidTimeDifferenceException();

        this.name = name;
        this.dateReachedResponsibility = dateReachedResponsibility;
        this.dateStartedPrayer = dateStartedPrayer;
        this.dateStartedFasting = dateStartedFasting;
    }

    public String getName() {
        return this.name;
    }

    public Date getDateReachedResponsibility() {
        return this.dateReachedResponsibility;
    }

    public Date getDateStartedFasting() {
        return this.dateStartedFasting;
    }

    public Date getDateStartedPrayer() {
        return this.dateStartedPrayer;
    }

    @Override
    public String toString() {
        // TODO
        return ("name="+this.name+","+
                "dateReachedResponsibility="+this.dateReachedResponsibility.getYear()+"/"+
                this.dateReachedResponsibility.getMonth()+"/"+this.dateReachedResponsibility.getDate()+","+
                this.dateStartedPrayer.getYear()+"/"+this.dateStartedPrayer.getMonth()+"/"+
                this.dateStartedPrayer.getDate()+","+
                this.dateStartedFasting.getYear());
    }

//    public String writeXml() throws RuntimeException {
//        XmlSerializer serializer = Xml.newSerializer();
//        StringWriter writer = new StringWriter();
//        try {
//            serializer.setOutput(writer);
//            serializer.startDocument("UTF-8", true);
//            serializer.startTag("", HesapciXmlManager.XMLTAG_USER_DATA);
//            serializer.startTag("", HesapciXmlManager.XMLTAG_USER);
//            serializer.attribute("", HesapciXmlManager.XMLATTRIBUTE_NAME, this.name);
//
//            serializer.startTag("", HesapciXmlManager.XMLTAG_DATE_REACHED_RESPONSIBILITY);
//            serializer.attribute("", HesapciXmlManager.XMLATTRIBUTE_YEAR, new String("" + this.dateReachedResponsibility.getYear()));
//            serializer.attribute("", HesapciXmlManager.XMLATTRIBUTE_MONTH, new String(""+this.dateReachedResponsibility.getMonth()));
//            serializer.attribute("", HesapciXmlManager.XMLATTRIBUTE_DAY, new String(""+this.dateReachedResponsibility.getDay()));
//            serializer.endTag("", HesapciXmlManager.XMLTAG_DATE_REACHED_RESPONSIBILITY);
//
//            serializer.startTag("", HesapciXmlManager.XMLTAG_DATE_STARTED_PRAYER);
//            serializer.attribute("", HesapciXmlManager.XMLATTRIBUTE_YEAR, new String("" + this.dateStartedPrayer.getYear()));
//            serializer.attribute("", HesapciXmlManager.XMLATTRIBUTE_MONTH, new String(""+this.dateStartedPrayer.getMonth()));
//            serializer.attribute("", HesapciXmlManager.XMLATTRIBUTE_DAY, new String(""+this.dateStartedPrayer.getDay()));
//            serializer.endTag("", HesapciXmlManager.XMLTAG_DATE_STARTED_PRAYER);
//
//            serializer.startTag("", HesapciXmlManager.XMLTAG_DATE_STARTED_FASTING);
//            serializer.attribute("", HesapciXmlManager.XMLATTRIBUTE_YEAR, new String(""+this.dateStartedFasting.getYear()));
//            serializer.endTag("", HesapciXmlManager.XMLTAG_DATE_STARTED_FASTING);
//
//            serializer.endTag("", HesapciXmlManager.XMLTAG_USER);
//            serializer.endTag("", HesapciXmlManager.XMLTAG_USER_DATA);
//            serializer.endDocument();
//            return writer.toString();
//        }
//        catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
}